package it.demo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.sink.RichSinkFunction;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

/**
 * Author itcast
 * Desc
 * 使用自定义sink将数据保存到MySQL
 */
public class SinkDemo02_MySQL_4 {
    public static void main(String[] args) throws Exception {
        //1.env
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //2.Source
        DataStream<Student> studentDS = env.fromElements(new Student(null, "tonyma", 18));
        //3.Transformation
        //4.Sink
        studentDS.addSink(new MySQLSink());

        //5.execute
        env.execute();
    }
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class Student {
        private Integer id;
        private String name;
        private Integer age;
    }

    public static class MySQLSink extends RichSinkFunction<Student> {
          private   Connection conn =null;
          private PreparedStatement ps =null;

          @Override
          public void open(Configuration parameters) throws Exception {
             //加载驱动 开启连接
             //Class forname
             conn =DriverManager.getConnection("jdbc:mysql://192.168.10.221:3306/bigdata?useDDL=false","root","123456");
            String sql ="INSERT INTO 't_student' (id,name,age) VALUES(null,?, ?)";
               ps = conn.prepareStatement(sql);
          }

          @Override
            public void invoke(Student value,Context context) throws Exception {
              //给ps?设置具体值
              ps.setString(1,value.getName());
              ps.setInt(2,value.getAge());
              //执行sql
              ps.executeUpdate();
          }

          @Override
        public void close()throws Exception {
                if(ps!=null){
                    ps.close();
                }
                if(conn!=null){
                    conn.close();
                }
          }
    }
}